<?php

/**
 * Description of Geocoding
 *
 * @author Alisson Chiquitto<chiquitto@chiquitto.com.br>
 */
class ZFKiller_Google_Geocoding {
    public static function pegarLatitudeLongitudePorEndereco(array $options) {
        $resultado = self::fazerConsultaPorEndereco($options);
        
        return $resultado['results'][0]['geometry']['location'];
    }
    
    public static function fazerConsultaPorEndereco(array $options) {
        $url = 'http://maps.googleapis.com/maps/api/geocode/json?' . http_build_query(array(
            'address' => $options['address'],
            'sensor' => 'false',
        ));
        
        // Create a stream
		$opts = array(
			'http' => array(
				'method' => 'GET',
				'header' => array(),
			)
		);

		//$opts['http']['header'][] = 'Accept-Language:pt-BR,pt;q=0.8,en-US;q=0.6,en;q=0.4';
		//if (isset($_SERVER['HTTP_USER_AGENT'])) {
		//	$opts['http']['header'][] = 'User-Agent:' . $_SERVER['HTTP_USER_AGENT'];
		//}
		$opts['http']['header'][] = 'Cache-Control:no-cache';
		$opts['http']['header'] = join("\r\n", $opts['http']['header']);

		$context = stream_context_create($opts);
		$content = file_get_contents($url, false, $context);
        
        return json_decode($content, 1);
    }
}